/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package callback.client;

import callback.serveur.CallbackServeurInterface;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Hippolyte
 */
public class CallbackClient {

    public static CallbackServeurInterface proxy;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        try {

            System.setSecurityManager(new RMISecurityManager());
            String registryURL = "rmi://localhost/callback";
            proxy = (CallbackServeurInterface) Naming.lookup(registryURL);
            System.out.println("Le serveur dit ");
            System.out.println("Abonnemenent pour les callbacks.");
            CallbackClientInterface objCallback = new CallbackClientImpl();

            

            //abonnement
            Thread.sleep(240 * 1000);

            //attente calcul du serveur !
            //proxy.recupererCalculDePi

            System.out.println("Desabonnement pour les callbacks.");
            proxy.desabonnement(objCallback);


        } catch (NotBoundException ex) {
            Logger.getLogger(CallbackClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(CallbackClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Logger.getLogger(CallbackClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException ex) {
            Logger.getLogger(CallbackClient.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public static String interroger() {
        return interroger(true);
    }

    public static String interroger(boolean abonnement) {
        String message = null;

        try {

            System.setSecurityManager(new RMISecurityManager());
//            String registryURL = "rmi://192.168.5.73/callback";
            String registryURL = "rmi://localhost/callback";
            proxy = (CallbackServeurInterface) Naming.lookup(registryURL);

            
            CallbackClientInterface objCallback = new CallbackClientImpl();

            if (abonnement) {
                System.out.println("Abonnemenent pour les callbacks.");
                proxy.abonnement(objCallback);
            }
            message = proxy.calculPi(objCallback);

            //abonnement
            Thread.sleep(1000);

            //attente calcul du serveur !
            //proxy.recupererCalculDePi

            if (abonnement) {
                System.out.println("Desabonnement pour les callbacks.");
                proxy.desabonnement(objCallback);
            }


        } catch (NotBoundException ex) {
            Logger.getLogger(CallbackClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(CallbackClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Logger.getLogger(CallbackClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException ex) {
            Logger.getLogger(CallbackClient.class.getName()).log(Level.SEVERE, null, ex);
        }

        return message;
    }
}
